package cn.iocoder.yudao.module.asasc.controller.admin.salesreport;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.asasc.controller.admin.salesreport.vo.SalesReportPageReqVO;
import cn.iocoder.yudao.module.asasc.controller.admin.salesreport.vo.SalesReportRespVO;
import cn.iocoder.yudao.module.asasc.controller.admin.salesreport.vo.SalesReportSaveReqVO;
import cn.iocoder.yudao.module.asasc.dal.dataobject.salesreport.SalesReportDO;
import cn.iocoder.yudao.module.asasc.service.salesreport.SalesReportService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - ASA管理系统")
@RestController
@RequestMapping("/asasc/sales-report")
@Validated
public class SalesReportController {

    @Resource
    private SalesReportService salesReportService;

    @PostMapping("/create")
    @Operation(summary = "创建ASA管理系统")
    @PreAuthorize("@ss.hasPermission('asasc:sales-report:create')")
    public CommonResult<Integer> createSalesReport(@Valid @RequestBody SalesReportSaveReqVO createReqVO) {
        return success(salesReportService.createSalesReport(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ASA管理系统")
    @PreAuthorize("@ss.hasPermission('asasc:sales-report:update')")
    public CommonResult<Boolean> updateSalesReport(@Valid @RequestBody SalesReportSaveReqVO updateReqVO) {
        salesReportService.updateSalesReport(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ASA管理系统")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('asasc:sales-report:delete')")
    public CommonResult<Boolean> deleteSalesReport(@RequestParam("id") Integer id) {
        salesReportService.deleteSalesReport(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ASA管理系统")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('asasc:sales-report:query')")
    public CommonResult<SalesReportRespVO> getSalesReport(@RequestParam("id") Integer id) {
        SalesReportDO salesReport = salesReportService.getSalesReport(id);
        return success(BeanUtils.toBean(salesReport, SalesReportRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ASA管理系统分页")
    @PreAuthorize("@ss.hasPermission('asasc:sales-report:query')")
    public CommonResult<PageResult<SalesReportRespVO>> getSalesReportPage(@Valid SalesReportPageReqVO pageReqVO) {
        PageResult<SalesReportDO> pageResult = salesReportService.getSalesReportPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SalesReportRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ASA管理系统 Excel")
    @PreAuthorize("@ss.hasPermission('asasc:sales-report:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSalesReportExcel(@Valid SalesReportPageReqVO pageReqVO,
                                       HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SalesReportDO> list = salesReportService.getSalesReportPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ASA管理系统.xls", "数据", SalesReportRespVO.class,
                BeanUtils.toBean(list, SalesReportRespVO.class));
    }

}